<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @author Administrator
 *        
 */
class File extends CI_Controller{
	
	protected $class_name;
	protected $dwt_list;
	protected $dwt_info;
	protected $myModel;
	protected $datas;
	
	function __construct() {
		parent::__construct ();
		$this->class_name = strtolower ( __CLASS__ );
		
		$this->datas ['full_page'] = true;
		$this->datas ['page_title'] = "上传管理";
		$this->datas ['my_position'] = $this->class_name;
		$this->datas ['my_router'] = $this->class_name;
		
		$this_model = $this->class_name . '_model';
		$this->load->model ( $this_model );
		$this->myModel = $this->$this_model;
		
	}
	/**
	 * 仅作为ZS链接模型用
	 */
	protected function link_model() {
		exit ();
		$this->load->model ( 'uploader_model' );
	}
	function index()
	{
		exit ();
	}
	function act(){
		
	}
	/**
	 * 文件上传接口
	 * @param string $class
	 */
	public function uploader($action = '')
	{
		$content = ''; $message = ''; $append_data = array();
		$post = $this->input->post();


		if(empty($action)){
			$action 	= isset($post['action']) ? trim($post['action']) : 'none';
		}
		$item_id 	= isset($post['itemid']) ? intval($post['itemid']) : 0;
		$sessid 	= isset($post['sessid']) ? trim($post['sessid']) : '';
		
		if($action == 'none'){
			make_json_error('非法操作！['.$action.']');
		}		
 		//make_json_error("ERR:$action".json_encode($post));
		//$image_path = "/uploadTmp/".$file_name;
		//格式化图片路径
		$image_path = $this->myModel->reformat_image_name($action, 'Original', $_FILES['Filedata']['name']);
		move_upload_file($_FILES['Filedata']['tmp_name'], full_static_path($image_path));
		//if($item_id > 0){
//			switch ($action){
//				case 'canteen_image':
//					$this->load->model('Canteen_meal_model');
//					$result = $this->Canteen_meal_model->update_image($item_id, $image_path);
//					break;
//				default:
//					$result['error'] = 300;
//					$result['message'] = '非法操作！['.$action.']';
//					break;
//			}
	//	}else{
			//id 为 0 存入临时文件表
			$result = $this->myModel->save_temp_file($action, $image_path);
			$append_data = $result['append_data'];
            //如果是头像就移除原图 取时 取大图 和缩略图即可
            $action_arr = array('hospital_user');
            if(in_array($action, $action_arr)){
                 $img_data = $result['append_data'];                 
                 if($img_data['format_original'] != $img_data['format_image']){
                     $this->myModel->del_img_url($image_path);
                 }                 
            }
	//	}
		
		$append_data['format_original'] = full_static_url($image_path);
		$append_data['action'] = $action;//必选字段
		if($result['error'] > 0){
			make_json_error($result['message']."\r\n[Err:".$result['error']."]");
		}else{
			make_json_result($content, $message, $append_data);
		}
	}
	
}

?>